//
//  CYNEWFabuVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/9/26.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYNEWFabuVC.h"
#import "WQLPaoMaView.h"
#import "LEUnitView+LE.h"
#import "CYCreditTool.h"
#import "CustomIOSAlertView.h"
#import "UIImageView+WebCache.h"
#import "MWPhotoBrowser.h"
#import "LEPhotoBrowserTool.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "UIButton+WebCache.h"
#import "CYTool.h"
#import "AddressView.h"
#import "CYDatePickerView.h"
#import "LEImagePicker.h"
#import "LEImagePickerTool.h"
#import "MBProgressHUD+LE.h"
#import "LEBarButtonItem.h"
#import "CYDiscoveryHttpTool.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "CYHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYNetworkCollectRecord.h"
#import "CYJumpsTool.h"
#import "CYWEITUOHelpVC.h"
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import "DCPaymentView.h"
#import "CYDES3Util.h"
#import "CYHelpXieyiVC.h"
#import "CYHelpShoufeiVC.h"
#import "CYXieYiLookVC.h"



@interface CYNEWFabuVC ()<UIScrollViewDelegate, CYDatePickerViewDelegate,UITextFieldDelegate,UITextViewDelegate>
{
     WQLPaoMaView *paoma;
}
@property (weak, nonatomic) IBOutlet UIView *paomaView;
@property (weak, nonatomic) IBOutlet UIView *zhengjuphotoView;
@property (nonatomic, strong) NSMutableArray *zhengjuphotoUrls;
@property (weak, nonatomic) IBOutlet UIButton *weituoyesBtn;
@property (weak, nonatomic) IBOutlet UIButton *weituonoBtn;
@property (weak, nonatomic) IBOutlet UIButton *sexmanBtn;
@property (weak, nonatomic) IBOutlet UIButton *sexwomanBtn;


@property (weak, nonatomic) IBOutlet UIView *newpaomaView;

@property (weak, nonatomic) IBOutlet UITextField *weiyueremnameTF;
@property (weak, nonatomic) IBOutlet UITextField *weiyuerenphoneTF;
@property (weak, nonatomic) IBOutlet UITextField *weiyuerenidcardTF;
@property (weak, nonatomic) IBOutlet UITextField *weiyuerenaddressTF;
@property (weak, nonatomic) IBOutlet UITextField *weiyuerenqqTF;
@property (weak, nonatomic) IBOutlet UITextField *jiekuanTF;
@property (weak, nonatomic) IBOutlet UITextField *jiekuantimeTF;
@property (weak, nonatomic) IBOutlet UITextField *huankuanTF;
@property (weak, nonatomic) IBOutlet UITextField *huankuantimeTF;
@property (weak, nonatomic) IBOutlet UITextField *yongjinTF;
@property (weak, nonatomic) IBOutlet UITextField *titleTF;
@property (weak, nonatomic) IBOutlet UITextView *weiyuemiaoshuTF;



@property (weak, nonatomic) CYDatePickerView *datePicker;
@property (strong, nonatomic) NSDate *lendDate;
@property (strong, nonatomic) NSDate *shouldPayDate;
@property (strong, nonatomic) NSDate *payDate;
@property (strong, nonatomic) NSString *lendDateStr;
@property (strong, nonatomic) NSString *shouldPayDateStr;
@property (strong, nonatomic) NSString *payDateStr;

@property (weak, nonatomic) IBOutlet UIView *cuishoubuchongView; //默认hidden

@property (weak, nonatomic) IBOutlet UIButton *sfzzmBtn;
@property (weak, nonatomic) IBOutlet UIButton *sfzfmBtn;
@property (nonatomic , strong) NSString *sfzzmStr;
@property (nonatomic , strong) NSString *sfzfmStr;

//地区选择器
@property (nonatomic, strong) UIToolbar *toolBar;
@property(nonatomic, strong) NSString *selectedCity;
@property(nonatomic, strong) NSString *selectedDistrict;
@property(nonatomic, strong) NSString *selectedProvince;
@property (nonatomic, strong) AddressView *addressView;

//委托的lab改变
@property (weak, nonatomic) IBOutlet UILabel *idcardlab;
@property (weak, nonatomic) IBOutlet UILabel *phonelab;

@property (weak, nonatomic) IBOutlet UILabel *addresslab;
@property (weak, nonatomic) IBOutlet UILabel *qqlab;
@property (weak, nonatomic) IBOutlet UILabel *jiekuanlab;
@property (weak, nonatomic) IBOutlet UILabel *jiekuantimelab;
@property (weak, nonatomic) IBOutlet UILabel *huankuanlab;
@property (weak, nonatomic) IBOutlet UILabel *huankuantimelab;
@property (weak, nonatomic) IBOutlet UILabel *yongjinlab;

@property (nonatomic , strong) NSString *isweituoStr;//1委托，0不委托默认发布
@property (nonatomic, strong) NSString *sexStr;//1为男，0为女

@property (nonatomic, strong) NSString *yongjinStr;

@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSString *pmoney;
@property (nonatomic, strong) NSString *proportion;
@property (nonatomic, strong) NSString *priceStr;
@property (nonatomic, strong) NSNumber *managePrice;

@property (nonatomic, strong) NSString *isfreeStr;



@end

@implementation CYNEWFabuVC

- (void)viewDidLoad {
    [super viewDidLoad];
     _isweituoStr = @"0";
     
     CYDatePickerView *picker = [[CYDatePickerView alloc] initWithFrame:CGRectMake(0, CYScreeSize.height, 0, 0)];
     [self.view addSubview:picker];
     picker.delegate = self;
     self.datePicker = picker;
     
     _zhengjuphotoUrls = [[NSMutableArray alloc]init];
     
     self.addressView = [[AddressView alloc] init];
     self.weiyuerenaddressTF.inputView = self.addressView;
     self.weiyuerenaddressTF.inputAccessoryView = self.toolBar;
     
     _weituoyesBtn.layer.cornerRadius = 5;
     _weituonoBtn.layer.cornerRadius = 5;
     _sexmanBtn.layer.cornerRadius = 5;
     _sexwomanBtn.layer.cornerRadius = 5;
     
     paoma = [[WQLPaoMaView alloc]initWithFrame:CGRectMake(30, 0, self.newpaomaView.width, self.newpaomaView.height) withTitle:@"温馨提示：选择委托催收前，请认真阅读催收协议 温馨提示：选择委托催收前，请认真阅读催收协议 温馨提示：选择委托催收前，请认真阅读催收协议 "];
     [self.newpaomaView addSubview:paoma];
     [paoma start];
     
     UIButton *helpbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
     [helpbutton addTarget:self action:@selector(helpclick) forControlEvents:UIControlEventTouchUpInside];
//     [helpbutton setBackgroundColor:[UIColor blackColor]];
     
     [self.newpaomaView addSubview:helpbutton];
     
     //键盘回收delegate初始化
     _weiyueremnameTF.delegate = self;
     _weiyuerenphoneTF.delegate = self;
     _weiyuerenidcardTF.delegate = self;
     _weiyuerenqqTF.delegate = self;
     _jiekuanTF.delegate = self;
     _huankuanTF.delegate = self;
     _titleTF.delegate = self;
     _weiyuemiaoshuTF.delegate = self;
     
     [self setupUploadView];
     
     LEBarButtonItem *submit = [[LEBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_done_white_24dp"] andSelImage:nil andTitle:@"提交" target:self action:@selector(submitclick)];
     [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: submit,nil]];
     
     [self loadPrice];
     
    // Do any additional setup after loading the view from its nib.
}

-(void)loadPrice{
     CYUser *user = [CYLoginHttpTool userData].user;
     [CYHttpTool postWithURL:@"http://www.credit-manage.com:8058/Collectionsys/getbalanceforuser.htm" params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          _priceStr = jsoninfo.data;
          _managePrice = jsoninfo.data;
          
     } failure:^(NSError *error) {
          CYAlertView(@"网络错误");
     }];
     
     
}

#pragma mark - UITextView Delegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
     if ([text isEqualToString:@"\n"]) {
          [textView resignFirstResponder];
          return NO;
     }
     return YES;
}

-(void)submitclick{
     
//     CYUser *user = [CYLoginHttpTool userData].user;
     NSMutableDictionary *param = [self paramDic];
     if (param) {
          [MBProgressHUD showMessage:nil toView:self.view];
          
          [CYHttpTool postWithURL:[CYURL shared].addNetworkCollectRecord params:param success:^(id json) {
               [MBProgressHUD hideHUDForView:self.view];
               
               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//               NSString *recordId = jsoninfo.data;
               
               if (jsoninfo.isSuccess) {
                    if (_isweituoStr.intValue == 0) {
                         //普通发布返回
                         CYAlertView(jsoninfo.message);
                         [self.navigationController popViewControllerAnimated:YES];
                         return;
                    }
//                    if (_isweituoStr && _isweituoStr.intValue == 1) {
//                         NSString *lendtimeStr = [NSString stringWithFormat:@"%ld", (long)[_lendDate timeIntervalSince1970]];
//                         CYLog(@"timeSp:%@",lendtimeStr     ); //时间戳的值
//                         
//                         NSString *repaytimeStr = [NSString stringWithFormat:@"%ld", (long)[_shouldPayDate timeIntervalSince1970]];
//                         CYLog(@"repaytimeStr = %@",repaytimeStr);
//                         
//                         //判断条件
//                         if (_weiyuerenphoneTF.text.length == 0) {
//                              CYAlertView(@"请输入违约人姓名");
//                              return;
//                         }
//                         if (_weiyuerenphoneTF.text.length == 0) {
//                              CYAlertView(@"请输入违约人手机号");
//                              return;
//                         }
//                         if (!_sexStr) {
//                              CYAlertView(@"请选择性别");
//                              return;
//                         }
//                         if (_weiyuerenaddressTF.text.length == 0) {
//                              CYAlertView(@"请选择违约人所在地区");
//                              return;
//                         }
//                         if (_jiekuanTF.text.length == 0 ) {
//                              CYAlertView(@"请输入借款金额");
//                              return;
//                         }
//                         if (!lendtimeStr) {
//                              CYAlertView(@"请选择借款时间");
//                              return;
//                         }
//                         if (!_lendDate) {
//                              CYAlertView(@"请输入应还金额");
//                              return;
//                         }
//                         if (!repaytimeStr) {
//                              CYAlertView(@"请输入应还时间");
//                              return;
//                         }
//                         if (!_yongjinStr) {
//                              CYAlertView(@"未得出佣金，请按要求填写信息");
//                              return;
//                         }
//                         if (_zhengjuphotoUrls.count == 0) {
//                              CYAlertView(@"请最少填写一张图片");
//                              return;
//                         }
//                         
//                         if (!_sfzzmStr) {
//                              _sfzzmStr = @"";
//                              
//                         }
//                         if (!_sfzfmStr) {
//                              _sfzfmStr = @"";
//                              
//                         }
//                         if (_weiyuerenidcardTF.text.length == 0 ) {
//                              _weiyuerenidcardTF.text = @"XXXXXXXXXXXXXXXXXX";
//                         }
//                         if (_weiyuemiaoshuTF.text.length == 0) {
//                              CYAlertView(@"请输入描述");
//                              return;
//                         }
//                         if (_titleTF.text.length == 0) {
//                              CYAlertView(@"请输入标题");
//                              return;
//                         }
//                         if (!jsoninfo.message) {
//                              CYAlertView(@"保存失败！");
//                              return;
//                         }
//                         
//                         //需要委托
//                         //保存测试
//                         //数据处理开始
//                         NSString *str = nil;
//                         //字符串
//                         NSMutableString *string = [[NSMutableString alloc] init];
//                         str = [string JSONString];
//                         str = [_zhengjuphotoUrls JSONString];
//                         CYLog(@"str = %@",str);
//                         NSString *datastr = [NSString stringWithString:str];
//                         
//                         //付费查询
//                         [CYHttpTool postWithURL:@"http://www.credit-manage.com:8058/Collectionsys/deductionExpense.htm" params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
//                              CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//                              CYLog(@"jsoninfo = %@",jsoninfo);
//                              NSString *statueSte = jsoninfo.data;
//                              _isfreeStr = statueSte;
//                              NSString *messageStr = jsoninfo.message;
//                              // 1付费，0免费
//                              if (statueSte.intValue == 1) {
//                                   //付费
//                                   
//                                   UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"平台管理费：%@元",_priceStr] message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//                                   [alertview show];
//                                   [alertview showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//                                        if (buttonIndex == 1) {
//                                             
//                                             DCPaymentView *payAlert = [[DCPaymentView alloc]init];
//                                             payAlert.detail = @"请输入交易密码";
//                                             [payAlert show];
//                                             payAlert.completeHandle = ^(NSString *inputPwd) {
//                                                  NSString *desstr = [CYDES3Util encrypt:inputPwd];
//                                                  if (!recordId) {
//                                                       CYAlertView(@"黑名单发布失败！请重试");
//                                                       return ;
//                                                  }
//                                                            [CYHttpTool postWithURL:[CYURL shared].saveAppDefaulter params:@{@"userId":user.userId,@"token":user.token,@"sex":_sexStr,@"loanAmount":_jiekuanTF.text,@"name":_weiyueremnameTF.text,@"phone":_weiyuerenphoneTF.text,@"certificateNo":_weiyuerenidcardTF.text,@"homeAddress":_weiyuerenaddressTF.text,@"repaymentAmount":_huankuanTF.text,@"defaulterQq":_weiyuerenqqTF.text,@"content":_weiyuemiaoshuTF.text,@"title":_titleTF.text,@"commissionAmount":_yongjinStr,@"frontPhotos":_sfzzmStr,@"oppositePhotos":_sfzfmStr,@"evidencePhotos":datastr,@"otherPhotos":@"",@"lendTime":_jiekuantimeTF.text,@"repaymentTime":_huankuantimeTF.text,@"id":recordId,@"Proportion":_proportion,@"isFree":@"0",@"dealPassword":desstr} success:^(id json) {
//                                                                 [MBProgressHUD hideHUDForView:self.view];
//                                                                 CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//                                                                 CYLog(@"jsoninfo = %@",jsoninfo);
//                                                                 if (jsoninfo.isSuccess) {
//                                                                      CYAlertView(jsoninfo.message);                                       [self.navigationController popViewControllerAnimated:YES];
//                                                                 }else{
//                                                                      CYAlertView(jsoninfo.message);
//                                                                      
//                                                                 }
//                                                                 
//                                                                 
//                                                            } failure:^(NSError *error) {
//                                                                 [MBProgressHUD hideHUDForView:self.view];
//                                                                 CYAlertView(@"网络错误");
//                                                                 CYLog(@"error = %@",error);
//                                                                 
//                                                            }];
//                                                  
//                                             };
//                                             
//                                        }
//                                        
//                                   }];
//                                   
//                              }else{
//                                   
//                                   UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"提交需要扣除平台管理费：%@元",_priceStr] message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//                                   [alertview show];
//                                   [alertview showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//                                        if (buttonIndex == 1) {
//                                             //本次无需扣费，直接调用提交
//                                             //                    CYAlertView(@"本次无需扣费")
//                                             [MBProgressHUD showMessage:nil toView:self.view];
//                                             [CYHttpTool postWithURL:[CYURL shared].saveAppDefaulter params:@{@"userId":user.userId,@"token":user.token,@"sex":_sexStr,@"loanAmount":_jiekuanTF.text,@"name":_weiyueremnameTF.text,@"phone":_weiyuerenphoneTF.text,@"certificateNo":_weiyuerenidcardTF.text,@"homeAddress":_weiyuerenaddressTF.text,@"repaymentAmount":_huankuanTF.text,@"defaulterQq":_weiyuerenqqTF.text,@"content":_weiyuemiaoshuTF.text,@"title":_titleTF.text,@"commissionAmount":_yongjinStr,@"frontPhotos":_sfzzmStr,@"oppositePhotos":_sfzfmStr,@"evidencePhotos":datastr,@"otherPhotos":@"",@"lendTime":_jiekuantimeTF.text,@"repaymentTime":_huankuantimeTF.text,@"id":recordId,@"Proportion":_proportion,@"isFree":@"1"} success:^(id json) {
//                                                  [MBProgressHUD hideHUDForView:self.view];
//                                                 
//                                                  CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
//                                                  CYLog(@"jsoninfo = %@",jsoninfo);
//                                                  if (jsoninfo.isSuccess) {
//                                                       CYAlertView(jsoninfo.message);                                       [self.navigationController popViewControllerAnimated:YES];
//                                                  }else{
//                                                       CYAlertView(jsoninfo.message);
//                                                       
//                                                  }
//                                                  
//                                                  
//                                             } failure:^(NSError *error) {
//                                                  
//                                                  [MBProgressHUD hideHUDForView:self.view];
//                                                  CYAlertView(@"网络错误");
//                                                  CYLog(@"error = %@",error);
//                                                  
//                                             }];
//                                             
//                                        }
//                                        
//                                   }];
//                                   
//                              }
//                         } failure:^(NSError *error) {
//                                      [MBProgressHUD hideHUDForView:self.view];
//                              CYAlertView(@"网络错误");
//                         }];
//                         
//                    }

                    
               }
               
               
               
          } failure:^(NSError *error) {
               [MBProgressHUD hideHUDForView:self.view];
               CYAlertView(@"网络错误");
               
          }];
          
          
     }
     
}

- (NSMutableDictionary *)paramDic {
     CYUser *user = [CYLoginHttpTool userData].user;
     if (user) {
          CYNetworkCollectRecord *record = [CYNetworkCollectRecord new];
          if (self.weiyueremnameTF.text && !self.weiyueremnameTF.text.isEmpty) {
               record.name = self.weiyueremnameTF.text;
          } else {
               [MBProgressHUD showError:@"请输入姓名"];
               return nil;
          }
          if (self.weiyuerenphoneTF.text && !self.weiyuerenphoneTF.text.isEmpty) {
               record.phone = self.weiyuerenphoneTF.text;
          }
          if (self.weiyuerenidcardTF.text && !self.weiyuerenidcardTF.text.isEmpty) {
               record.certificateNo = self.weiyuerenidcardTF.text;
          }
          if (!_sexStr) {
               [MBProgressHUD showError:@"请选择性别"];
               return nil;
          }
          if (self.selectedProvince && !self.selectedProvince.isEmpty) {
               record.province = self.selectedProvince;
               record.city = self.selectedCity;
               record.district = self.selectedDistrict;
               
          } else {
               [MBProgressHUD showError:@"请选择违约人所在地区"];
               return nil;
          }
          if (self.titleTF.text && !self.titleTF.text.isEmpty) {
               record.title = self.titleTF.text;
          } else {
               [MBProgressHUD showError:@"请输入标题"];
               return nil;
          }
          if (self.weiyuemiaoshuTF.text && !self.weiyuemiaoshuTF.text.isEmpty) {
               record.defaultContent = self.weiyuemiaoshuTF.text;
          } else {
               [MBProgressHUD showError:@"请输入违约详情"];
               return nil;
          }
          if (self.zhengjuphotoUrls.count) {
               record.picAddress = self.zhengjuphotoUrls;
          } else {
               [MBProgressHUD showError:@"请上传证据照片"];
               return nil;
          }
          NSMutableDictionary *param = record.keyValues;
          param[@"userId"] = user.userId;
          param[@"token"] = user.token;
          if (_weiyuerenqqTF.text&&_weiyuerenqqTF.text.length) {
               param[@"qq"] = _weiyuerenqqTF.text;
          }
          return param;
     } else {
          [CYJumpsTool jumpToLogin:self];
     }
     return nil;
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
     //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
     [_weiyueremnameTF resignFirstResponder];
     [_weiyuerenphoneTF resignFirstResponder];
     [_weiyuerenidcardTF resignFirstResponder];
     [_weiyuerenqqTF resignFirstResponder];
     [_jiekuanTF resignFirstResponder];
     [_huankuanTF resignFirstResponder];
     
     return YES;
     
}

- (UIToolbar *)toolBar{
     if (_toolBar == nil) {
          self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
          _toolBar.barTintColor= CYColor(95, 200, 100, 1);
          UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
          UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
          _toolBar.items = @[item1, item2];
     }
     return _toolBar;
}

- (void)click {
     if (_weiyuerenaddressTF.isFirstResponder) {
          [_weiyuerenaddressTF resignFirstResponder];
          self.weiyuerenaddressTF.text = [NSString stringWithFormat:@"%@-%@-%@",self.addressView.province,self.addressView.city,self.addressView.area];
          //            NSLog(@"text = %@",_areaText);
          _selectedCity = self.addressView.city;
          _selectedProvince = self.addressView.province;
          _selectedDistrict = self.addressView.area;
     }
     if([_weiyuerenaddressTF.text  isEqual: @"(null)-(null)-(null)"]) {
          NSString *p = @"北京市";
          NSString *c = @"北京辖区";
          NSString *a = @"东城区";
          _weiyuerenaddressTF.text = [NSString stringWithFormat:@"%@-%@-%@",p,c,a];
          _selectedCity = c;
          _selectedProvince = p;
          _selectedDistrict = a;
     }
}

- (void)setupAttribute {
     self.weiyuemiaoshuTF.layer.borderColor = [UIColor grayColor].CGColor;
     self.weiyuemiaoshuTF.layer.borderWidth = .5f;
}


- (void)setupUploadView {
     //证据照
     [LEUnitView imageScrollView:self.zhengjuphotoView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
               [_zhengjuphotoUrls addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
}


- (IBAction)sfzzmClick:(id)sender {
     
     [self.view.window endEditing:YES];
     [LEImagePickerTool select2ImageWithController:self edit:YES completion:^(UIImage *image) {
          [MBProgressHUD showMessage:@"处理中..." toView:self.view];
          [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
               [MBProgressHUD hideHUDForView:self.view animated:YES];
               _sfzzmStr = imagePath;
               [_zhengjuphotoUrls addObject:imagePath];
               
              
          } failure:^(NSString *message, NSError *error) {
              
          }];
     }];

}

- (IBAction)sfzfmClick:(id)sender {
     
     [self.view.window endEditing:YES];
     [LEImagePickerTool select2ImageWithController:self edit:YES completion:^(UIImage *image) {
          [MBProgressHUD showMessage:@"处理中..." toView:self.view];
          [CYDiscoveryHttpTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
               [MBProgressHUD hideHUDForView:self.view animated:YES];
               _sfzfmStr = imagePath;
               [_zhengjuphotoUrls addObject:imagePath];
          } failure:^(NSString *message, NSError *error) {
               
          }];
     }];

}


-(void)helpclick{
     
     CYLog(@"beclick");
     CYWEITUOHelpVC *help = [[CYWEITUOHelpVC alloc]init];
     [self.navigationController pushViewController:help animated:YES];
     
}


#pragma mark - 日期选择
//借款时间
- (IBAction)jiekuantimeClick:(id)sender {
     
     [UIView animateWithDuration:.4 animations:^{
          self.datePicker.transform = CGAffineTransformMakeTranslation(0, -self.datePicker.height);
     }];
     
     self.datePicker.minDate = nil;
     self.datePicker.maxDate = self.shouldPayDate ? self.shouldPayDate : self.payDate;//优先返回本该还款时间，maxDate<本该还款时间<=实际还款时间
     
     __weak typeof (self)weakSelf = self;
     [self.datePicker selectedDate:^(NSDate *date, NSString *dateStr) {
          
//          UIButton *btn = (UIButton *)sender;
//          [btn setTitle:dateStr forState:UIControlStateNormal];
          weakSelf.lendDate = date;
          weakSelf.lendDateStr = dateStr;
          
          _jiekuantimeTF.text = dateStr;
          CYLog(@"%@", dateStr);
          
     }];
     [self.view.window endEditing:YES];

     
}

//还款时间

- (IBAction)huankuantimeClick:(id)sender {
     if (_huankuanTF.text.length == 0) {
          CYAlertView(@"请输入应还金额");
          return;
     }
     if (_jiekuantimeTF.text.length == 0) {
          CYAlertView(@"请选择借款时间");
          return;
     }

     [UIView animateWithDuration:.4 animations:^{
          self.datePicker.transform = CGAffineTransformMakeTranslation(0, -self.datePicker.height);
     }];
     
     self.datePicker.minDate = self.lendDate;
     self.datePicker.maxDate = self.payDate;
     
     __weak typeof (self)weakSelf = self;
     [self.datePicker selectedDate:^(NSDate *date, NSString *dateStr) {
//          UIButton *btn = (UIButton *)sender;
//          [btn setTitle:dateStr forState:UIControlStateNormal];
          weakSelf.shouldPayDate    = date;
          weakSelf.shouldPayDateStr = dateStr;
          _huankuantimeTF.text = dateStr;
          CYLog(@"%@", dateStr);
          [self loadyongjin];
     }];
     [self.view.window endEditing:YES];
     
}

-(void) loadyongjin{
     //调用接口计算佣金
     if (_isweituoStr.intValue == 1) {
          CYUser *user = [CYLoginHttpTool userData].user;
          [CYHttpTool postWithURL:@"http://www.credit-manage.com:8058/Collectionsys/getCommission.htm" params:@{@"userId":user.userId,@"token":user.token,@"stoptime":_huankuantimeTF.text,@"money":_huankuanTF.text} success:^(id json) {
               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
               CYLog(@"jsoninfo = %@",jsoninfo);
               if (jsoninfo.isSuccess) {
                    NSDictionary *dic = jsoninfo.data;
                    _yongjinStr = [dic objectForKey:@"pmoney"];
                    _yongjinTF.text = _yongjinStr;
                    _proportion = [dic objectForKey:@"proportion"];
                    
               }else{
                    CYAlertView(jsoninfo.message);
               }
               
          } failure:^(NSError *error) {
               CYLog(@"error = %@",error);
               CYAlertView(@"网络错误");
               
          }];
     }else{
          
          _yongjinTF.text = @"无需支付佣金";
          
     }
     
     
     
     
}

#pragma mark - CYDatePickerViewDelegate
- (void)datePickerView:(CYDatePickerView *)view date:(NSDate *)date dateFormatter:(NSString *)formatter {
     //隐藏遮盖
//     self.cover.hidden = YES;
     
     //退下日期选中框
     [UIView animateWithDuration:.3 animations:^{
          self.datePicker.transform = CGAffineTransformIdentity;
     }];
}

- (IBAction)weituoyesClick:(id)sender {
     
     CYUser *user = [CYLoginHttpTool userData].user;
     if (user.certificateExamineStatus.intValue != 2 ) {
          CYAlertView(@"请先进行实名认证！");
          return;
          
     }
     
     
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"同意委托发布协议，进行发布" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"<委托催收协议>",@"确认", nil];
                    [alert show];
                    [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
                         if (buttonIndex == 1) {
                              CYXieYiLookVC *xieyi = [[CYXieYiLookVC alloc]init];
                              [self.navigationController pushViewController:xieyi animated:YES];
                              
                         }if (buttonIndex == 2) {
                              _isweituoStr = @"1";
                              [_weituonoBtn setImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
                              [_weituoyesBtn setImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
                              _cuishoubuchongView.hidden = NO;
                              
                              //     _idcardlab.text = @"* 身份证号";
                              _phonelab.text = @"* 违约人手机号";
                              _jiekuanlab.text = @"* 借款金额";
                              _jiekuantimelab.text = @"* 借款时间";
                              _huankuanlab.text = @"* 应还金额";
                              _huankuantimelab.text = @"* 应还时间";
                              _yongjinlab.text = @"* 催收佣金";
                              _addresslab.text = @"* 违约人所在地区";
                         }

                    }];
     

     
}

- (IBAction)weituonoClick:(id)sender {
     _isweituoStr = @"0";
     [_weituoyesBtn setImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_weituonoBtn setImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
     _cuishoubuchongView.hidden = YES;
     
//     _idcardlab.text = @"   身份证号";
     _phonelab.text = @"   违约人手机号";
     _jiekuanlab.text = @"   借款金额";
     _jiekuantimelab.text = @"   借款时间";
     _huankuanlab.text = @"   应还金额";
     _huankuantimelab.text = @"   应还时间";
     _yongjinlab.text = @"   催收佣金";
     _addresslab.text = @"   违约人所在地区";
     
     
}


- (IBAction)sexmanClick:(id)sender {
     _sexStr = @"1";
     [_sexwomanBtn setImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_sexmanBtn setImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];

     
     
}


- (IBAction)sexwomenClick:(id)sender {
     _sexStr = @"0";
     [_sexmanBtn setImage:[UIImage imageNamed:@"icon_1"] forState:UIControlStateNormal];
     [_sexwomanBtn setImage:[UIImage imageNamed:@"icon_2"] forState:UIControlStateNormal];
    
}

- (IBAction)shoufeiClick:(id)sender {
     CYHelpShoufeiVC *shoufei = [[CYHelpShoufeiVC alloc]init];
     [self.navigationController pushViewController:shoufei animated:YES];
     
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
